﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Messaging;
using System.Text;

namespace System
{
    public static class MessageQueueExtensionMethods
    {
        /// <summary>
        /// 验证队列是否为空
        /// </summary>
        /// <param name="queue"></param>
        /// <returns></returns>
        public static bool IsEmpty(this MessageQueue queue)
        {
            bool isQueueEmpty = false;

            // Connect to a queue.
            try
            {
                // Set Peek to return immediately.
                queue.Peek(new TimeSpan(0));

                // If an IOTimeout was not thrown, there is a message 
                // in the queue.
                isQueueEmpty = false;
            }
            catch (MessageQueueException e)
            {
                if (e.MessageQueueErrorCode == MessageQueueErrorCode.IOTimeout)
                {
                    // No message was in the queue.
                    isQueueEmpty = true;
                }
            }

            return isQueueEmpty;
        }
    }
}
